# -*- coding: utf-8 -*-
# @Time: 2021/8/19 11:07
# @File: arg-26.py
# @DESC: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
# 删除有序链表的重复项


# 移动到列表最后的位置，然后移除
def func1(nums) -> int:
    flag = 0
    while flag < len(nums):
        if flag == 0 or nums[flag] != temp:
            temp = nums[flag]
            flag += 1
        else:
            # nums.remove(nums[flag])
            for i in range(flag, len(nums) - 1):
                temp = nums[i]
                nums[i] = nums[i + 1]
                nums[i + 1] = temp
            nums.pop()
    return len(nums)


# 移除指定元素
def func2(nums):
    flag = 0
    while flag < len(nums):
        if flag == 0 or nums[flag] != temp:
            temp = nums[flag]
            flag += 1
        else:
            nums.remove(nums[flag])
    return len(nums)


# 这种方式有问题，因为移除了之后，对于剩下的数组位置就是错位的
def func3(nums):
    check_list = []
    for index, i in enumerate(nums):
        if i in check_list:
            nums.remove(nums[index])
        else:
            check_list.append(i)
    print(nums)


# 官方解法：快慢指针
def func4(nums):
    # 快指针；用于遍历
    # 慢指针：用于记录交换的位置
    lent = len(nums)
    if lent <= 1:
        return lent
    flag = 1
    for i in range(1, lent):
        if nums[i] - nums[i - 1] != 0:
            nums[flag] = nums[i]
            flag += 1
    return flag


nums = [1, 1, 2, 2, 3, 3, 3, 3]

r = func4(nums)
print(r)
